﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exceltodata
{
    class GameDataTableParser : IDisposable
    {
        private byte[] xorScale = new byte[] { 45, 66, 38, 55, 23, 254, 9, 165, 90, 19, 41, 45, 201, 58, 55, 37, 254, 185, 165, 169, 19, 171 };

        private Dictionary<string, int> m_FileNameDic = null;
        private int m_Row = 0;
        private int m_Column = 0;
        private string[,] m_GameData = null;
        private string[] m_FileName = null;
        public string[] FileName {
            get { return m_FileName; }
        }
        private int m_CurRowNo = 3;
        /// <summary>
        /// 是否结束
        /// </summary>
        public bool Eof {
            get {
                return m_CurRowNo == m_Row;
            }
        }
        /// <summary>
        /// 下一条
        /// </summary>
        public void Next() {
            if (Eof) return;
            m_CurRowNo++;

        }
        /// <summary>
        /// 获取字段值
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public string GetFieldValue(string fieldName) {
            try {
                if (m_CurRowNo < 3 || m_CurRowNo >= m_Row) return null;
                return m_GameData[m_CurRowNo, m_FileNameDic[fieldName]];
            } catch{
                return null;
            }
        }

        public GameDataTableParser(string path) {
            m_FileNameDic = new Dictionary<string, int>();
            byte[] buffer = null;
            using (FileStream fs = new FileStream(path, FileMode.Open)) {
                buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
            }
            //压缩

            //解密
            int iScaleLen = xorScale.Length;
            for (int i = 0; i < buffer.Length; i++) {
                buffer[i] = (byte)(buffer[i]^xorScale[i%iScaleLen]);
            }
            //写入文件
            using (MMO_MemoryStream ms = new MMO_MemoryStream(buffer)) {
                m_Row = ms.ReadInt();
                m_Column = ms.ReadInt();
                m_GameData = new String[m_Row, m_Column];
                m_FileName = new string[m_Column];
                for (int i = 0; i < m_Row; i++) {
                    for (int j = 0; j < m_Column; j++) {
                        string str = ms.ReadUTF8String();
                        if (i == 0)
                        {
                            //读取字段
                            m_FileName[j] = str;
                            m_FileNameDic[str] = j;
                        }
                        else if (i > 2) {
                            //读取内容
                            m_GameData[i, j] = str;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 释放
        /// </summary>
        void IDisposable.Dispose()
        {
            m_FileNameDic.Clear();
            m_FileNameDic = null;
            m_FileName = null;
            m_GameData = null;
        }
    }
}
